<?php

namespace Addons\ThreeWords;

use Common\Controller\Addon;
use Think\Db;

/**
 * Class ThreeWordsAddon  三字评论插件
 * @package Addons\ThreeWords
 * @author:xjw129xjt xjt@ourstu.com
 */

class ThreeWordsAddon extends Addon
{

    public $info = array(
        'name' => 'ThreeWords',
        'title' => '三字评论',
        'description' => '三字评论插件',
        'status' => 1,
        'author' => 'xjw129xjt',
        'version' => '0.1.1'
    );

    public $addon_path = './Addons/ThreeWords/';

    /**
     * 配置列表页面
     * @var unknown_type
     */
    public $admin_list = array(
        'model' => 'Config',
        'order' => 'find_in_set( name ,"MAIL_TYPE,MAIL_SMTP_HOST,MAIL_SMTP_PORT,MAIL_SMTP_USER,MAIL_SMTP_PASS,MAIL_SMTP_CE,WEB_SITE") ',
        'map' => array('name' => array('in', array(0 => 'MAIL_TYPE', 1 => 'MAIL_SMTP_HOST', 2 => 'MAIL_SMTP_PORT', 3 => 'MAIL_SMTP_USER', 4 => 'MAIL_SMTP_PASS', 5 => 'MAIL_SMTP_CE', 6 => 'WEB_SITE')))
    );
    public $custom_adminlist = 'adminlist.html';

    /**
     * (non-PHPdoc)
     * 安装函数
     * @see \Common\Controller\Addons::install()
     */
    public function table_name()
    {
        $db_prefix = C('DB_PREFIX');
        return $db_prefix;
    }


    public function install()
    {
        return true;
    }

    /**
     * (non-PHPdoc)
     * 卸载函数
     * @see \Common\Controller\Addons::uninstall()
     */
    public function uninstall()
    {
        return true;
    }

    //实现的钩子
    public function AdminIndex($param)
    {

    }

    protected  function ThreeWordModel(){
        return  D('Addons://ThreeWords/ThreeWords');
    }
    protected  function CommentModel(){
        return  D('Addons://ThreeWords/ThreeWordsComment');
    }
    public function threeWords($param){
        $limit = 10;
        $words = $this->ThreeWordModel()->getAllWords();
        $map['model']= $param['model'];
        $map['row_id']= $param['row_id'];
        $comment = $this->CommentModel()->getComment($map,1,$limit);

        shuffle($words);
        $words = array_slice($words,0,36);
        $this->assign('words',$words);
        $this->assign('comment',$comment);
        $this->assign($param);
        $map['status']=1;
        $totalCount = $this->CommentModel()->where($map)->count();
        $pageCount = ceil($totalCount / $limit);
        $html = getPageHtml('changeThreeWordsPage', $pageCount, '', 1);
        $this->assign('pageHtml',$html);

        $this->display('threewords');
    }
}